package com.xuxin.baby.database;

import com.xuxin.baby.App;
import com.xuxin.baby.bean.ReaderBean;
import com.xuxin.baby.bean.VideoBean;
import com.xuxin.baby.utils.BaseUtils;

import java.util.List;

/**
 * Created by xu_xin
 * Created on 2020/11/16 0016
 * 数据库操作
 */
public class DBControl {

    /**
     * 购物车
     */
    public static void insertCart(VideoBean videoBean) {
        CartBean cartBean = new CartBean();
        cartBean.setUid(videoBean.getResult().getId());
        cartBean.setImage(videoBean.getResult().getImageUrl());
        cartBean.setTitle(videoBean.getResult().getTitle());
        cartBean.setContent(videoBean.getResult().getSummary());
        cartBean.setPrice(videoBean.getResult().getBeans());
        cartBean.setType(0);
        App.getInstance().getDaoSession().getCartBeanDao().insertOrReplaceInTx(cartBean);
    }

    public static List<CartBean> queryCart() {
        return App.getInstance().getDaoSession().getCartBeanDao().queryBuilder().list();
    }

    public static void deleteCartById(long id) {
        App.getInstance().getDaoSession().getCartBeanDao().deleteByKey(id);
    }

    public static void deleteAllCart() {
        App.getInstance().getDaoSession().getCartBeanDao().deleteAll();
    }

    /**
     * 历史足迹
     */
    public static void insertHistory(VideoBean videoBean) {
        HistoryBean historyBean = new HistoryBean();
        historyBean.setUid(videoBean.getResult().getId());
        historyBean.setImage(videoBean.getResult().getImageUrl());
        historyBean.setTitle(videoBean.getResult().getTitle());
        historyBean.setContent(videoBean.getResult().getSummary());
        historyBean.setType(0);
        App.getInstance().getDaoSession().getHistoryBeanDao().insertOrReplaceInTx(historyBean);
    }

    public static void insertHistory(ReaderBean readerBean) {
        HistoryBean historyBean = new HistoryBean();
        historyBean.setUid(readerBean.getResult().getId());
        historyBean.setImage(readerBean.getResult().getImageUrl());
        historyBean.setTitle(readerBean.getResult().getTitle());
        historyBean.setContent(BaseUtils.dispose(readerBean.getResult().getSummary()));
        historyBean.setType(1);
        App.getInstance().getDaoSession().getHistoryBeanDao().insertOrReplaceInTx(historyBean);
    }

    public static List<HistoryBean> queryHistory() {
        return App.getInstance().getDaoSession().getHistoryBeanDao().queryBuilder().list();
    }

    public static void deleteAllHistory() {
        App.getInstance().getDaoSession().getHistoryBeanDao().deleteAll();
    }

    /**
     * 搜索记录
     */
    public static void insertSearch(String search) {
        SearchBean searchBean = new SearchBean();
        searchBean.setTitle(search);
        App.getInstance().getDaoSession().getSearchBeanDao().insertOrReplaceInTx(searchBean);
    }

    public static List<SearchBean> querySearch() {
        return App.getInstance().getDaoSession().getSearchBeanDao().queryBuilder().list();
    }

    public static void deleteAllSearch() {
        App.getInstance().getDaoSession().getSearchBeanDao().deleteAll();
    }


}